$(function () {
  $.ajax({
    method: 'get',
    url: BigNew.user_detail,
    data: {},
    success: (res) => {
      console.log(res);
      //const { email, nickname, password, userPic, username } = res.data;
      // console.log(email, nickname, password, userPic, username);
      //对象通过 for in 循环遍历
      //获取值 obj[key]
      for (let key in res.data) {
        if (key !== 'userPic') {
          $(`[name="${key}"]`).val(res.data[key]);
        }
      }
      $('.user_pic').prop('src', res.data.userPic);
    }
  })

  //文件预览
  $('.user_pic').click(() => {
    $('[name="userPic"]').click();
  });
  $('[name="userPic"]').change(() => {
    const file = $('[name="userPic"]').get(0).files[0];
    //console.dir(file);
    //调用浏览器提供的API转换成浏览器可以识别的地址
    const url = URL.createObjectURL(file);
    //console.log(url);
    //把图片写入到src属性上
    $('.user_pic').prop('src', url);
  });


  //编辑个人信息
  $('[type="submit"]').click((e) => {
    //阻止默认事件

    e.preventDefault();
    const form = document.getElementById('form');
    const fd = new FormData(form);
    fd.forEach((value) => {
      console.log(value);
    });
    //提交数据
    $.ajax({
      method: 'post',
      url: BigNew.user_edit,
      data: fd,
      contentType: false,
      processData: false,
      success: (res) => {
        // console.log(res);
        if (res.code == 200) {
          //注意 需要用Live server打开
          window.parent.location.reload();
        }
      }
    })
  });
})